package com.Shoping.controller;

import cn.dsna.util.images.ValidateCode;
import com.Shoping.entity.Constants;
import com.Shoping.entity.User;
import com.Shoping.service.ShoppingService;
import com.Shoping.service.UserService;
import com.Shoping.service.impl.ShoppingServiceImpl;
import com.Shoping.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

@WebServlet(value = "/user")
public class UserController extends BaseServlet {
    public String login(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");

        UserService userService = new UserServiceImpl();
        User user = userService.selectByNamePwd(userName,password);
        if(user!=null){
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            return "1";
        }else{
            return "0";
        }
    }

    public void creatCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
        ValidateCode code = new ValidateCode(100, 35, 5, 20);
        String code1 = code.getCode();
        HttpSession session = request.getSession();
        session.setAttribute("code",code1);
        code.write(response.getOutputStream());
    }

    public String selectUser(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        String userCode = request.getParameter("userCode");
        UserService userService = new UserServiceImpl();
        User user = userService.selectByUsername(userCode);
        if(user!=null){
            return "1";
        }
        return "0";
    }

    public String createUser(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        String userCode = request.getParameter("userCode");
        String newPassword = request.getParameter("newPassword");
        String rePassword = request.getParameter("rePassword");
        String verify = request.getParameter("verify");
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("code");
        if(!code.equals(verify)){
            request.setAttribute("userCode",userCode);
            request.setAttribute("newPassword",newPassword);
            request.setAttribute("rePassword",rePassword);
            request.setAttribute("verify_tip","验证码错误");
            return Constants.FORWARD+"/ProgramJSP/register.jsp";
        }
        UserService userService = new UserServiceImpl();
        ShoppingService shoppingService = new ShoppingServiceImpl();

        userService.createUser(userCode,newPassword);
        User user1 = userService.selectByNamePwd(userCode, newPassword);
        shoppingService.createShopping(user1.getuId());
        request.setAttribute("flag_register",1);
        return Constants.FORWARD+"/ProgramJSP/register.jsp";
    }

    public String updatePassword(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String s = user.getuPassword();
        if(!password.equals(s)){
            request.setAttribute("pwd_tip","原密码错误");
            return Constants.FORWARD+"ProgramJSP/updatePwd.jsp";
        }
        String verify = request.getParameter("verify");
        String code = (String) session.getAttribute("code");
        if(!code.equals(verify)){
            request.setAttribute("password",password);
            request.setAttribute("verify_tip","验证码错误");
            return Constants.FORWARD+"/ProgramJSP/updatePwd.jsp";
        }
        UserService userService = new UserServiceImpl();
        userService.updatePassowrd(password,user.getuId());
        request.setAttribute("sign",1);
        return Constants.FORWARD+"/ProgramJSP/updatePwd.jsp";
    }

    public String logout(HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return Constants.REDIRECT+"/homePage.jsp";
    }
}
